package cn.thomas.clock.utils;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class CopyUtils {
    public static void copy(Object source, Object dest) throws Exception {
        // 获取属性
        BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),Object.class);
        PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

        BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(),Object.class);
        PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

        try {
            for (PropertyDescriptor propertyDescriptor : sourceProperty) {

                for (PropertyDescriptor descriptor : destProperty) {

                    if (propertyDescriptor.getName().equals(descriptor.getName()) && propertyDescriptor.getPropertyType() == descriptor.getPropertyType()) {
                        // 调用source的getter方法和dest的setter方法
                        descriptor.getWriteMethod().invoke(dest, propertyDescriptor.getReadMethod().invoke(source));
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("属性复制失败:" + e.getMessage());
        }
    }
    public static void main(String[] args) throws Exception{
        /*SysUserVO s = new SysUserVO();


        UserSaveVO a = new UserSaveVO();
        a.setMobile("21");
        a.setEmail("qq.com");
        a.setPassword("123456");
        a.setPhone("18383841510");
        a.setUserId(11L);

        CopyUtils.Copy(a,s);

        System.out.println(s);
        System.out.println("-----------------------------------------");
        System.out.println(a);*/
    }
}